/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.ui.graphical.tools;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;


/**
 * Input to event game converter
 * 
 * @author Matt
 */
public class InputConverter implements MouseListener, KeyListener {

	
	/* -------------- field ------------- */

	/** a graphical appearance of the game's deck */
	private Deck deck;
	
	/** Current graphical zone */
	private GraphicsZone selectedGZone;
	
	/** Current graphical Card */
	private GraphicsCard selectedGCard;
	
	/** Collection of user input listeners */
	private Collection<UserInputListener> listeners = new ArrayList<UserInputListener>();

	
	/* ----------- Constructor ---------- */

	/**
	 * Constructor for objects of class InputConverter.
	 * 
	 * @param deck
	 *            the Deck
	 */
	public InputConverter(Deck deck){
		this.deck = deck;
	}

	
	/* ------------- Methods ------------- */
	
	/**
	 * Add a UserInputListener to the InputToEventGameConverter
	 * 
	 * @param l
	 *            the UserInputListener
	 */
	public void addUserInputListener(UserInputListener l) {
		listeners.add(l);
	}

	
	/**
	 * Remove a UserInputListener from the InputToEventGameConverter
	 * 
	 * @param l
	 *            the UserInputListener
	 */
	public void removeUserInputListener(UserInputListener l) {
		listeners.remove(l);
	}	
	
	
	/**
	 * Update the current card and current zone which have been clicked
	 * 
	 * @param x the x-coordinate
	 * @param y the y-coordinate
	 */
	private void updateCardAndZoneClicked(int x, int y){

		GraphicsZone gZone = null;
		GraphicsCard gCard = null;
		selectedGZone = null;
		selectedGCard = null;
			
		// get the current Graphical Zone where user has clicked
		for(int i = 0 ; i < deck.getGraphicsZoneList().size() ; i++ ) {
			
			gZone = deck.getGraphicsZoneList().get(i);
			
			if ( ( ((x > gZone.getX()) && (x < gZone.getX() + gZone.getWidth())) &&
				((y > gZone.getY()) && (y < gZone.getY() + gZone.getHeight())) ) ){
				
				selectedGZone = gZone;
				break;
			}
		}
		
		// get the current Graphical Card where user has clicked
		for(int i = 0 ;  i < selectedGZone.getGCardList().size() ; i++ ) {
			
			gCard = selectedGZone.getGCardList().get(i);
			
			if ( ( ((x > gCard.getX()) && (x < gCard.getX() + gCard.getWidth())) &&
				((y > gCard.getY()) && (y < gCard.getY() + gCard.getHeight())) ) ){
				
				selectedGCard = gCard;
				break;
			}
		}
	}
	
	
	/* ------------- Mouse ------------ */
	
	/**
	 *  Invoked when the mouse button has been clicked.
	 *  
	 *  @param e the MouseEvent to listen to
	 */
	public void mouseClicked(MouseEvent e){
		
		// update current card and current zone which has been clicked
		updateCardAndZoneClicked(e.getX(),e.getY());

		for (UserInputListener uil : listeners)
			uil.onSelectedCardChanged(selectedGCard);
		
		deck.repaint();
	}
	
	
	/* --------------- Key -------------- */
	
	/**
	 *  Invoked when a key has been pressed.
	 *  
	 *  @param e the KeyEvent to listen to
	 */
	public void keyPressed(KeyEvent e){
		
		for (UserInputListener uil : listeners)
			uil.onKeyPressed(e, deck);
		
		deck.repaint();
	}
	
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	
}
